package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author stone
 * @date 2022/11/15 21:24
 */
@RestController
@RequestMapping("upload")
public class UploadController {

    //localhost:8080/upload/file
    @RequestMapping("file")
    public BaseRespVo file(MultipartFile file) throws IOException {
        String name = file.getName();
        String originalFilename = file.getOriginalFilename();
        String contentType = file.getContentType();
        long size = file.getSize();
        //InputStream inputStream = file.getInputStream();
        //将文件保存D:\tmp\ 并且呢文件名叫banner.png
        File saveFile = new File("D:\\tmp", originalFilename);
        file.transferTo(saveFile);

        return BaseRespVo.ok();
    }
    //localhost:8080/upload/files
    @RequestMapping("files")
    public BaseRespVo files(MultipartFile[] files) throws IOException {
        for (MultipartFile file : files) {
            String originalFilename = file.getOriginalFilename();
            File saveFile = new File("D:\\tmp", originalFilename);
            file.transferTo(saveFile);
        }
        return BaseRespVo.ok();
    }


}
